//
//  MHSearchShowViewController.m
//  ManHua
//
//  Created by 江子安 on 15/10/29.
//  Copyright © 2015年 蓝欧科技. All rights reserved.
//

#import "MHSearchShowViewController.h"
#import <AFNetworking.h>
#import "MHComicIntroduct.h"
#import "MHComicIntroductionCell.h"
#import "MHComicDetailViewController.h"

#define kResultURL @"http://112.124.96.190:9090/bukamanhua5appjobs/ComicHandle.ashx"

@interface MHSearchShowViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation MHSearchShowViewController

- (NSMutableArray *)dataArr
{
    if (!_dataArr) {
        _dataArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataArr;
}

// 最终搜索的数据
- (void)requestSearchResultDataWithString:(NSString *)string
{
    [self.dataArr removeAllObjects];
    //    ?name=%E6%96%97%E7%BD%97%E5%A4%A7%E9%99%86&method=search&type=0
    NSString *name = [NSString stringWithCString:[string UTF8String] encoding:NSUTF8StringEncoding];
    NSDictionary *dict = [NSDictionary dictionaryWithObjects:@[name,@"search",@"0"] forKeys:@[@"name",@"method",@"type"]];
    [[AFHTTPRequestOperationManager manager] GET:kResultURL parameters:dict success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        NSArray *arr = responseObject;
        for (NSDictionary *comicDict in arr) {
            MHComicIntroduct *model = [[MHComicIntroduct alloc]init];
            [model setValuesForKeysWithDictionary:comicDict];
            [self.dataArr addObject:model];
        }
        
        [self.collectionView reloadData];
    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
        
    }];
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor whiteColor];
    [self requestSearchResultDataWithString:self.name];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    
    _collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    [_collectionView registerNib:[UINib nibWithNibName:@"MHComicIntroductionCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
    
    CGFloat margin = 20;
    CGFloat padding = 10;
    NSInteger row = 3;
    CGFloat itemW = (Width - 2 * padding - (row - 1) * padding) / row;
    CGFloat itemH = (itemW - padding) * 11.0 / 9.0 + 35;
    layout.itemSize = CGSizeMake(itemW, itemH);
    layout.sectionInset = UIEdgeInsetsMake(margin, padding, margin + 44, padding);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 10;
    
    
    [self.view addSubview:_collectionView];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArr.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MHComicIntroductionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    MHComicIntroduct *model = self.dataArr[indexPath.item];
    cell.comicIntroduct = model;
    return  cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MHComicIntroduct *model = self.dataArr[indexPath.item];
    
    MHComicDetailViewController *comicDetailVC = [[MHComicDetailViewController alloc]init];
    comicDetailVC.comicIntroduct = model;
    [self.navigationController pushViewController:comicDetailVC animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
